/*
 * Copyright (C) 2021 Baidu, Inc. All Rights Reserved.
 */
package com.chukun.rpc.protocol.serialization;

import java.util.Map;
import java.util.PrimitiveIterator;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>序列化工厂<p/>
 *
 * @author chukun
 * @version 1.0
 * @since 1.0-SNAPSHOT
 */
public class SerializationFactory {

    private static final Map<Integer, TinyRpcSerialization> SERIALIZATION_MAP;

    static {
        SERIALIZATION_MAP = new ConcurrentHashMap<Integer, TinyRpcSerialization>(2) {
            {
                put(SerializationTypeEnum.HESSIAN.getType(), new HessianSerialization());
                put(SerializationTypeEnum.JSON.getType(), new JsonSerialization());
            }
        };
    }

    private SerializationFactory() {}

    private static class SerializationFactoryHolder {
        private static final SerializationFactory INSTANCE = new SerializationFactory();
    }

    public static SerializationFactory getInstance() {
        return SerializationFactoryHolder.INSTANCE;
    }

    public TinyRpcSerialization getSerializationByType (int serializationType) {
        if (SERIALIZATION_MAP.containsKey(serializationType)) {
            return SERIALIZATION_MAP.get(serializationType);
        }
        throw new IllegalArgumentException("Serialization type is illegal, " + serializationType);
    }
}
